package com.example.service.impl;

import ch.qos.logback.classic.Logger;
import com.example.entity.PointDefend;
import com.example.mapp.PointDefendMapp;
import com.example.util.StringUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缓存
 */
@Component
public class PointDefendInfoCache {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(PointDefendInfoCache.class);

    private static Map<String, PointDefend> map = new HashMap<String, PointDefend>();

    @Autowired
    private PointDefendMapp pointDefendMapp;

    @PostConstruct
    public void initCache() {
        List<PointDefend> list = null;
        try {
            list = pointDefendMapp.findList(new PointDefend());
            if (list != null && list.size() > 0) {
                for (PointDefend point : list) {
                    map.put(point.getIdentification(), point);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public PointDefend getKeyByValue(String key) {
        if (StringUtil.isEmpty(key)) {
            return null;
        }
        for (Map.Entry<String, PointDefend> entry : map.entrySet()) {
            if (entry.getKey().equals(key)) {
                return entry.getValue();
            }
        }
        return null;
    }
}
